package com.qdsgjsfk.vision.ui;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.Toast;

import com.qdsgjsfk.vision.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import butterknife.OnClick;
//import cn.bertsir.zbar.Qr.ScanResult;
//import cn.bertsir.zbar.QrConfig;
//import cn.bertsir.zbar.QrManager;
//import cn.bertsir.zbar.view.ScanLineView;
import cn.bertsir.zbar.Qr.ScanResult;
import cn.bertsir.zbar.QrConfig;
import cn.bertsir.zbar.QrManager;
import cn.bertsir.zbar.view.ScanLineView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import com.qdsgjsfk.vision.base.BaseActivity;
import com.qdsgjsfk.vision.util.QrConfigUtil;
import com.qdsgjsfk.vision.util.UtilString;
import com.rest.client.SSLSocketClient;

import util.PreferencesHelper;

/**
 * Created by HP on 2019/11/27.
 */

public class SelectTypeActivity extends BaseActivity {

    public static int REQUEST_CODE_ADD_FACE = 6;
    private String taskId;

    @Override
    protected void initViews() {

    }

    @Override
    protected void initData() {
        taskId = getIntent().getStringExtra("taskId");
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_select_type;
    }

    @OnClick(R.id.btn_scan)
    void btn_scan() {
        QrManager.getInstance().init(QrConfigUtil.getQrConfig()).startScan(SelectTypeActivity.this, new QrManager.OnScanResultCallback() {
            @Override
            public void onScanSuccess(ScanResult result) {
                Log.e("onScanSuccess---", result.getContent());
//                Toast.makeText( getApplicationContext(), "内容：" + result.getContent()
//                        + "  类型：" + result.getType(), Toast.LENGTH_SHORT ).show();
                if (!UtilString.isEmpty(result.getContent())) {
                    try {
                        scanQRCode(result.getContent(), 1);
//                    JSONObject obj = new JSONObject(content);
//                    getTaskWorker(obj.optString("taskId"), obj.optString("studentId"));
                    } catch (Exception e) {
                        Toast.makeText(SelectTypeActivity.this, "二维码识别失败", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    @OnClick(R.id.btn_class)
    void btn_class() {
        Bundle bundle = new Bundle();
        bundle.putString("taskId", taskId);
        startActivity(CollectClassActivity.class, bundle);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_ADD_FACE && resultCode == RESULT_OK) {
            if (data != null) {
//                String content = data.getStringExtra(Constant.CODED_CONTENT);
//                try {
//                    scanQRCode(content,1);
////                    JSONObject obj = new JSONObject(content);
////                    getTaskWorker(obj.optString("taskId"), obj.optString("studentId"));
//                } catch (Exception e) {
//                    Toast.makeText(this, "二维码有误", Toast.LENGTH_SHORT).show();
//                }

            }
        }

    }


    private void scanQRCode(String url, int action) {//0 扫学生  1加人脸
        System.out.println(url);
        OkHttpClient okHttpClient = new OkHttpClient.Builder().sslSocketFactory( SSLSocketClient.getSSLSocketFactory())
                .hostnameVerifier(SSLSocketClient.getHostnameVerifier())
                .build();
        final Request request = new Request.Builder()
                .url(url)
                .addHeader("Authorization", PreferencesHelper.getInstance().getString(this, "token"))
                .get()//默认就是GET请求，可以不写
                .build();
        System.out.println(PreferencesHelper.getInstance().getString(this, "token"));
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                assert response.body() != null;
                String s = response.body().string();
                System.out.println(s);
                try {
                    JSONObject obj = new JSONObject(s);
                    JSONObject json = new JSONObject(obj.optString("data"));

                    int type = json.optInt("type");
                    if (type == 10) {
                        if (action == 1) {
                            String sid = json.optString("sid");
                            Intent intMain = new Intent(getApplicationContext(), FaceDetectorInfoActivity.class);
                            Bundle bd = new Bundle();
                            bd.putString("studentId", sid);
                            bd.putString("taskId", taskId);
                            bd.putInt("faceType", 0);
                            intMain.putExtras(bd);
                            startActivity(intMain);
                        }
                    } else if (type == 1) {
                        String taskId = json.optString("taskId");
                        String userId = json.optString("userId");
                        String username = json.optString("username");
                        Intent intMain = new Intent(getApplicationContext(), ReceiveActivity.class);
                        Bundle bd = new Bundle();
                        bd.putString("taskId", taskId);
                        bd.putString("userId", userId);
                        bd.putString("username", username);
                        intMain.putExtras(bd);
                        startActivity(intMain);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

    }
}
